{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-24T01:22:00.141516Z",
     "start_time": "2019-10-24T01:21:55.883843Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.DataFrame的结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-24T01:22:00.330951Z",
     "start_time": "2019-10-24T01:22:00.155422Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>记录数</th>\n",
       "      <th>city</th>\n",
       "      <th>credit_by</th>\n",
       "      <th>num</th>\n",
       "      <th>num_1</th>\n",
       "      <th>riqi</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>青岛市</td>\n",
       "      <td>HUABEI</td>\n",
       "      <td>24</td>\n",
       "      <td>132</td>\n",
       "      <td>20191005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>青岛市</td>\n",
       "      <td>YUEBAO</td>\n",
       "      <td>1</td>\n",
       "      <td>87</td>\n",
       "      <td>20190910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>YUANDONGRONGZU</td>\n",
       "      <td>24</td>\n",
       "      <td>272</td>\n",
       "      <td>20191007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>青岛市</td>\n",
       "      <td>TIANCHENGRONGZU</td>\n",
       "      <td>4</td>\n",
       "      <td>68</td>\n",
       "      <td>20190909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>HUABEI</td>\n",
       "      <td>187</td>\n",
       "      <td>250</td>\n",
       "      <td>20191001</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   记录数 city        credit_by  num  num_1      riqi\n",
       "0    1  青岛市           HUABEI   24    132  20191005\n",
       "1    1  青岛市           YUEBAO    1     87  20190910\n",
       "2    1  合肥市   YUANDONGRONGZU   24    272  20191007\n",
       "3    1  青岛市  TIANCHENGRONGZU    4     68  20190909\n",
       "4    1  合肥市           HUABEI  187    250  20191001"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "os.chdir(r'D:\\Pandas')\n",
    "# 设定最大列数和最大行数\n",
    "pd.set_option('max_columns',8,'max_row',10)\n",
    "dt = pd.read_csv('test.csv')\n",
    "dt.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 访问DataFrame的组件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:20:07.885417Z",
     "start_time": "2019-10-22T07:20:07.879433Z"
    }
   },
   "outputs": [],
   "source": [
    "# 提取列索引\n",
    "columns = dt.columns\n",
    "# 提取行索引\n",
    "index = dt.index\n",
    "# 提取数据\n",
    "data = dt.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:20:13.308914Z",
     "start_time": "2019-10-22T07:20:13.300937Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['记录数', 'city', 'credit_by', 'num', 'num_1', 'riqi'], dtype='object')"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:20:49.756920Z",
     "start_time": "2019-10-22T07:20:49.750965Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=484, step=1)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:20:54.846310Z",
     "start_time": "2019-10-22T07:20:54.837330Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, '青岛市', 'HUABEI', 24, 132, 20191005],\n",
       "       [1, '青岛市', 'YUEBAO', 1, 87, 20190910],\n",
       "       [1, '合肥市', 'YUANDONGRONGZU', 24, 272, 20191007],\n",
       "       ...,\n",
       "       [1, '合肥市', 'YUEBAO', 11, 247, 20190925],\n",
       "       [1, '青岛市', 'TIANCHENGRONGZU', 10, 135, 20190913],\n",
       "       [1, '合肥市', 'DELEKEJI', 1, 247, 20190925]], dtype=object)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:22:29.123097Z",
     "start_time": "2019-10-22T07:22:29.117115Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.indexes.base.Index"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#columns的类型\n",
    "type(columns) #pandas.core.indexes.base.Index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:23:16.043174Z",
     "start_time": "2019-10-22T07:23:16.038188Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.indexes.range.RangeIndex"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#index的类型\n",
    "type(index) #pandas.core.indexes.range.RangeIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:24:30.724777Z",
     "start_time": "2019-10-22T07:24:30.718791Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#data的类型\n",
    "type(data) #numpy.ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:30:49.500473Z",
     "start_time": "2019-10-22T07:30:49.493494Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断是不是子类型\n",
    "issubclass(pd.RangeIndex, pd.Index) #True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:33:20.557092Z",
     "start_time": "2019-10-22T07:33:20.550109Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['记录数', 'city', 'credit_by', 'num', 'num_1', 'riqi'], dtype=object)"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 访问index的值\n",
    "index.values\n",
    "# 访问columns的值\n",
    "columns.values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.理解数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:39:02.157568Z",
     "start_time": "2019-10-22T07:39:02.142576Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "记录数           int64\n",
       "city         object\n",
       "credit_by    object\n",
       "num           int64\n",
       "num_1         int64\n",
       "riqi          int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt = pd.read_csv('test.csv')\n",
    "# 各列的类型\n",
    "dt.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:39:05.320081Z",
     "start_time": "2019-10-22T07:39:05.311137Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int64     4\n",
       "object    2\n",
       "dtype: int64"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显示各类型的数量\n",
    "dt.get_dtype_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.选择一列数据，作为Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:41:57.350598Z",
     "start_time": "2019-10-22T07:41:57.341623Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      青岛市\n",
       "1      青岛市\n",
       "2      合肥市\n",
       "3      青岛市\n",
       "4      合肥市\n",
       "      ... \n",
       "479    合肥市\n",
       "480    合肥市\n",
       "481    合肥市\n",
       "482    青岛市\n",
       "483    合肥市\n",
       "Name: city, Length: 484, dtype: object"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选择city列\n",
    "dt['city']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:42:27.047854Z",
     "start_time": "2019-10-22T07:42:27.038880Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      青岛市\n",
       "1      青岛市\n",
       "2      合肥市\n",
       "3      青岛市\n",
       "4      合肥市\n",
       "      ... \n",
       "479    合肥市\n",
       "480    合肥市\n",
       "481    合肥市\n",
       "482    青岛市\n",
       "483    合肥市\n",
       "Name: city, Length: 484, dtype: object"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 也可以通过属性的方式选取\n",
    "dt.city"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:42:46.824348Z",
     "start_time": "2019-10-22T07:42:46.807385Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看类型\n",
    "type(dt.city)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:44:22.565428Z",
     "start_time": "2019-10-22T07:44:22.558448Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'city'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看选取的列的名字\n",
    "city = dt.city\n",
    "city.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:45:23.370407Z",
     "start_time": "2019-10-22T07:45:23.354450Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>city</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>青岛市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>青岛市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>合肥市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>青岛市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>合肥市</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  city\n",
       "0  青岛市\n",
       "1  青岛市\n",
       "2  合肥市\n",
       "3  青岛市\n",
       "4  合肥市"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 单列Series转换为DataFrame\n",
    "city.to_frame().head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.调用Series方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 准备"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T07:49:50.336310Z",
     "start_time": "2019-10-22T07:49:50.328357Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "471"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看Series所有不重复的指令\n",
    "s_attr_methods = set(dir(pd.Series))\n",
    "# 该集合的大小\n",
    "len(s_attr_methods)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T08:17:02.655027Z",
     "start_time": "2019-10-22T08:17:02.647082Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "458"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看DataFrame所有不重复的指令\n",
    "df_attr_methods = set(dir(pd.DataFrame))\n",
    "len(df_attr_methods)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-22T08:18:05.076553Z",
     "start_time": "2019-10-22T08:18:05.070565Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "400"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 这两个集合中有多少个共有的指令\n",
    "len(s_attr_methods & df_attr_methods)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 原理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:26:24.075251Z",
     "start_time": "2019-10-23T01:26:24.039316Z"
    }
   },
   "outputs": [],
   "source": [
    "# 选取city和num两列\n",
    "dt = pd.read_csv('test.csv')\n",
    "city = dt['city']\n",
    "num = dt['num']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:26:38.539559Z",
     "start_time": "2019-10-23T01:26:38.532568Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    青岛市\n",
       "1    青岛市\n",
       "2    合肥市\n",
       "3    青岛市\n",
       "4    合肥市\n",
       "Name: city, dtype: object"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看头部\n",
    "city.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:26:49.021523Z",
     "start_time": "2019-10-23T01:26:49.013547Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     24\n",
       "1      1\n",
       "2     24\n",
       "3      4\n",
       "4    187\n",
       "Name: num, dtype: int64"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:28:08.009338Z",
     "start_time": "2019-10-23T01:28:07.996373Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "合肥市    265\n",
       "青岛市    219\n",
       "Name: city, dtype: int64"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分别计数\n",
    "pd.set_option('max_rows',8)\n",
    "city.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:28:25.239273Z",
     "start_time": "2019-10-23T01:28:25.227304Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1      59\n",
       "2      39\n",
       "3      19\n",
       "8      18\n",
       "       ..\n",
       "104     1\n",
       "33      1\n",
       "105     1\n",
       "203     1\n",
       "Name: num, Length: 117, dtype: int64"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:29:37.171950Z",
     "start_time": "2019-10-23T01:29:37.164971Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "484"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city.size\n",
    "city.shape\n",
    "len(city)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:30:09.067672Z",
     "start_time": "2019-10-23T01:30:09.061691Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "484"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# city有多少非空值\n",
    "city.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:30:43.820789Z",
     "start_time": "2019-10-23T01:30:43.813778Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "484"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:31:35.039818Z",
     "start_time": "2019-10-23T01:31:35.020871Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17.0"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# num 的中位分位数\n",
    "num.quantile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:39:03.201601Z",
     "start_time": "2019-10-23T01:39:03.189666Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 203, 32.52066115702479, 17.0, 40.892648263026715, 15740)"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求最小值、最大值、平均值、中位数、标准差、总和\n",
    "num.min(),num.max(),num.mean(),num.median(),num.std(), \\\n",
    "num.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:46:03.141015Z",
     "start_time": "2019-10-23T01:46:03.125025Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    484.000000\n",
       "mean      32.520661\n",
       "std       40.892648\n",
       "min        1.000000\n",
       "25%        4.000000\n",
       "50%       17.000000\n",
       "75%       43.250000\n",
       "max      203.000000\n",
       "Name: num, dtype: float64"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 打印描述信息\n",
    "num.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:46:45.126726Z",
     "start_time": "2019-10-23T01:46:45.110769Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count     484\n",
       "unique      2\n",
       "top       合肥市\n",
       "freq      265\n",
       "Name: city, dtype: object"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:48:24.494227Z",
     "start_time": "2019-10-23T01:48:24.486252Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num.quantile(.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:49:57.136536Z",
     "start_time": "2019-10-23T01:49:57.124567Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1     1.0\n",
       "0.2     2.0\n",
       "0.3     6.0\n",
       "0.4     9.0\n",
       "0.5    17.0\n",
       "0.6    26.0\n",
       "0.7    37.1\n",
       "0.8    51.0\n",
       "0.9    99.5\n",
       "Name: num, dtype: float64"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 各个十分之一分位数\n",
    "pd.set_option('max_rows',10)\n",
    "num.quantile([.1,.2,.3,.4,.5,.6,.7,.8,.9])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:50:40.542484Z",
     "start_time": "2019-10-23T01:50:40.533508Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      False\n",
       "1      False\n",
       "2      False\n",
       "3      False\n",
       "4      False\n",
       "       ...  \n",
       "479    False\n",
       "480    False\n",
       "481    False\n",
       "482    False\n",
       "483    False\n",
       "Name: num, Length: 484, dtype: bool"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 非空值\n",
    "num.isnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:51:35.037817Z",
     "start_time": "2019-10-23T01:51:35.029839Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "484"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num_filled = num.fillna(0)\n",
    "num_filled.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:53:10.140515Z",
     "start_time": "2019-10-23T01:53:10.132538Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "484"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除缺失值\n",
    "num_dropped = num.dropna()\n",
    "num_dropped.size"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 更多"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:55:02.621783Z",
     "start_time": "2019-10-23T01:55:02.475177Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "合肥市    0.547521\n",
       "青岛市    0.452479\n",
       "Name: city, dtype: float64"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# value_counts(normalize=True) 可以返回频率\n",
    "city.value_counts(normalize=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:55:41.539732Z",
     "start_time": "2019-10-23T01:55:41.530754Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断是否有缺失值\n",
    "city.hasnans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:56:37.613816Z",
     "start_time": "2019-10-23T01:56:37.606832Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      True\n",
       "1      True\n",
       "2      True\n",
       "3      True\n",
       "4      True\n",
       "       ... \n",
       "479    True\n",
       "480    True\n",
       "481    True\n",
       "482    True\n",
       "483    True\n",
       "Name: city, Length: 484, dtype: bool"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断是否是非缺失值\n",
    "city.notnull()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 在Series上使用运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:58:45.973625Z",
     "start_time": "2019-10-23T01:58:45.965679Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.options.display.max_rows = 6\n",
    "5+9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:58:59.014758Z",
     "start_time": "2019-10-23T01:58:59.006779Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "256"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "4 ** 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:59:25.419162Z",
     "start_time": "2019-10-23T01:59:25.413179Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "not(5<9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T01:59:51.463531Z",
     "start_time": "2019-10-23T01:59:51.456551Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "6 in list(range(8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T02:00:37.271089Z",
     "start_time": "2019-10-23T02:00:37.263111Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{3}"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set([1,2,3]) & set([3,4,5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 准备\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:37:17.449051Z",
     "start_time": "2019-10-23T05:37:17.441075Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      132\n",
       "1       87\n",
       "2      272\n",
       "      ... \n",
       "481    247\n",
       "482    135\n",
       "483    247\n",
       "Name: num_1, Length: 484, dtype: int64"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score = dt['num_1']\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:37:53.900074Z",
     "start_time": "2019-10-23T05:37:53.888104Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      133\n",
       "1       88\n",
       "2      273\n",
       "      ... \n",
       "481    248\n",
       "482    136\n",
       "483    248\n",
       "Name: num_1, Length: 484, dtype: int64"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 每列值加1\n",
    "score + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:38:23.434112Z",
     "start_time": "2019-10-23T05:38:23.426134Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      264\n",
       "1      174\n",
       "2      544\n",
       "      ... \n",
       "481    494\n",
       "482    270\n",
       "483    494\n",
       "Name: num_1, Length: 484, dtype: int64"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 每列值乘以2\n",
    "score * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:38:54.404308Z",
     "start_time": "2019-10-23T05:38:54.393336Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       True\n",
       "1      False\n",
       "2       True\n",
       "       ...  \n",
       "481     True\n",
       "482     True\n",
       "483     True\n",
       "Name: num_1, Length: 484, dtype: bool"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断是否大于100\n",
    "score > 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:39:16.375104Z",
     "start_time": "2019-10-23T05:39:16.363139Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       True\n",
       "1       True\n",
       "2      False\n",
       "       ...  \n",
       "481    False\n",
       "482     True\n",
       "483    False\n",
       "Name: city, Length: 484, dtype: bool"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断是否等于字符串\n",
    "city == '青岛市'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 更多"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:40:17.455802Z",
     "start_time": "2019-10-23T05:40:17.448816Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      142\n",
       "1       97\n",
       "2      282\n",
       "      ... \n",
       "481    257\n",
       "482    145\n",
       "483    257\n",
       "Name: num_1, Length: 484, dtype: int64"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 利用通用函数实现加法\n",
    "score.add(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:40:51.328237Z",
     "start_time": "2019-10-23T05:40:51.320257Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      264\n",
       "1      174\n",
       "2      544\n",
       "      ... \n",
       "481    494\n",
       "482    270\n",
       "483    494\n",
       "Name: num_1, Length: 484, dtype: int64"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score.mul(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:41:37.059966Z",
     "start_time": "2019-10-23T05:41:37.049996Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      18\n",
       "1      12\n",
       "2      38\n",
       "       ..\n",
       "481    35\n",
       "482    19\n",
       "483    35\n",
       "Name: num_1, Length: 484, dtype: int64"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score.floordiv(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:42:35.185560Z",
     "start_time": "2019-10-23T05:42:35.177583Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       True\n",
       "1      False\n",
       "2       True\n",
       "       ...  \n",
       "481     True\n",
       "482     True\n",
       "483     True\n",
       "Name: num_1, Length: 484, dtype: bool"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score.gt(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:43:17.218196Z",
     "start_time": "2019-10-23T05:43:17.209205Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       True\n",
       "1       True\n",
       "2      False\n",
       "       ...  \n",
       "481    False\n",
       "482     True\n",
       "483    False\n",
       "Name: city, Length: 484, dtype: bool"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city.eq('青岛市')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:44:20.801184Z",
     "start_time": "2019-10-23T05:44:20.792208Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      2\n",
       "1      2\n",
       "2      2\n",
       "      ..\n",
       "481    2\n",
       "482    0\n",
       "483    2\n",
       "Name: num_1, Length: 484, dtype: int32"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 利用通用函数取模\n",
    "score.astype(int).mod(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:46:49.663184Z",
     "start_time": "2019-10-23T05:46:49.657201Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "type"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# a是int对象\n",
    "a = type(1)\n",
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:47:05.625507Z",
     "start_time": "2019-10-23T05:47:05.616531Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# a是pandas.core.series.Series对象\n",
    "a = type(score)\n",
    "a([1,2,3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7.串联Series方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:49:44.144686Z",
     "start_time": "2019-10-23T05:49:44.129725Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "225    14\n",
       "226    14\n",
       "204    13\n",
       "Name: num_1, dtype: int64"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# value_counts().head(3) 计数，查看前三\n",
    "score.value_counts().head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:50:25.311619Z",
     "start_time": "2019-10-23T05:50:25.304639Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 统计缺失值的数量\n",
    "score.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:51:19.622414Z",
     "start_time": "2019-10-23T05:51:19.617427Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# score的数据类型\n",
    "score.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:52:39.677894Z",
     "start_time": "2019-10-23T05:52:39.668951Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    132\n",
       "1     87\n",
       "2    272\n",
       "3     68\n",
       "4    250\n",
       "Name: num_1, dtype: int32"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 缺失值填充为0、转换为整型、查看前五\n",
    "score.fillna(0).astype(int).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 更多\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:53:38.800822Z",
     "start_time": "2019-10-23T05:53:38.793839Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 缺失值的比例\n",
    "score.isnull().mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:55:17.442220Z",
     "start_time": "2019-10-23T05:55:17.434242Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    132\n",
       "1     87\n",
       "2    272\n",
       "3     68\n",
       "4    250\n",
       "Name: num_1, dtype: int32"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用括号串联\n",
    "(score.fillna(0).astype(int).head())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8.使索引有意义\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:58:12.110856Z",
     "start_time": "2019-10-23T05:58:12.104872Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(484, 6)"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# set_index()给行索引命名\n",
    "dt.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T05:59:30.818418Z",
     "start_time": "2019-10-23T05:59:30.786504Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>记录数</th>\n",
       "      <th>credit_by</th>\n",
       "      <th>num</th>\n",
       "      <th>num_1</th>\n",
       "      <th>riqi</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>青岛市</th>\n",
       "      <td>1</td>\n",
       "      <td>HUABEI</td>\n",
       "      <td>24</td>\n",
       "      <td>132</td>\n",
       "      <td>20191005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>青岛市</th>\n",
       "      <td>1</td>\n",
       "      <td>YUEBAO</td>\n",
       "      <td>1</td>\n",
       "      <td>87</td>\n",
       "      <td>20190910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>合肥市</th>\n",
       "      <td>1</td>\n",
       "      <td>YUANDONGRONGZU</td>\n",
       "      <td>24</td>\n",
       "      <td>272</td>\n",
       "      <td>20191007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>合肥市</th>\n",
       "      <td>1</td>\n",
       "      <td>YUEBAO</td>\n",
       "      <td>11</td>\n",
       "      <td>247</td>\n",
       "      <td>20190925</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>青岛市</th>\n",
       "      <td>1</td>\n",
       "      <td>TIANCHENGRONGZU</td>\n",
       "      <td>10</td>\n",
       "      <td>135</td>\n",
       "      <td>20190913</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>合肥市</th>\n",
       "      <td>1</td>\n",
       "      <td>DELEKEJI</td>\n",
       "      <td>1</td>\n",
       "      <td>247</td>\n",
       "      <td>20190925</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>484 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      记录数        credit_by  num  num_1      riqi\n",
       "city                                            \n",
       "青岛市     1           HUABEI   24    132  20191005\n",
       "青岛市     1           YUEBAO    1     87  20190910\n",
       "合肥市     1   YUANDONGRONGZU   24    272  20191007\n",
       "...   ...              ...  ...    ...       ...\n",
       "合肥市     1           YUEBAO   11    247  20190925\n",
       "青岛市     1  TIANCHENGRONGZU   10    135  20190913\n",
       "合肥市     1         DELEKEJI    1    247  20190925\n",
       "\n",
       "[484 rows x 5 columns]"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt2 = dt.set_index('city')\n",
    "dt2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T06:01:22.964124Z",
     "start_time": "2019-10-23T06:01:22.929215Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>记录数</th>\n",
       "      <th>credit_by</th>\n",
       "      <th>num</th>\n",
       "      <th>num_1</th>\n",
       "      <th>riqi</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>青岛市</th>\n",
       "      <td>1</td>\n",
       "      <td>HUABEI</td>\n",
       "      <td>24</td>\n",
       "      <td>132</td>\n",
       "      <td>20191005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>青岛市</th>\n",
       "      <td>1</td>\n",
       "      <td>YUEBAO</td>\n",
       "      <td>1</td>\n",
       "      <td>87</td>\n",
       "      <td>20190910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>合肥市</th>\n",
       "      <td>1</td>\n",
       "      <td>YUANDONGRONGZU</td>\n",
       "      <td>24</td>\n",
       "      <td>272</td>\n",
       "      <td>20191007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>合肥市</th>\n",
       "      <td>1</td>\n",
       "      <td>YUEBAO</td>\n",
       "      <td>11</td>\n",
       "      <td>247</td>\n",
       "      <td>20190925</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>青岛市</th>\n",
       "      <td>1</td>\n",
       "      <td>TIANCHENGRONGZU</td>\n",
       "      <td>10</td>\n",
       "      <td>135</td>\n",
       "      <td>20190913</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>合肥市</th>\n",
       "      <td>1</td>\n",
       "      <td>DELEKEJI</td>\n",
       "      <td>1</td>\n",
       "      <td>247</td>\n",
       "      <td>20190925</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>484 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      记录数        credit_by  num  num_1      riqi\n",
       "city                                            \n",
       "青岛市     1           HUABEI   24    132  20191005\n",
       "青岛市     1           YUEBAO    1     87  20190910\n",
       "合肥市     1   YUANDONGRONGZU   24    272  20191007\n",
       "...   ...              ...  ...    ...       ...\n",
       "合肥市     1           YUEBAO   11    247  20190925\n",
       "青岛市     1  TIANCHENGRONGZU   10    135  20190913\n",
       "合肥市     1         DELEKEJI    1    247  20190925\n",
       "\n",
       "[484 rows x 5 columns]"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过index_col参数命名\n",
    "pd.read_csv('test.csv',index_col='city')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 更多\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T06:04:36.718260Z",
     "start_time": "2019-10-23T06:04:36.690333Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>city</th>\n",
       "      <th>记录数</th>\n",
       "      <th>credit_by</th>\n",
       "      <th>num</th>\n",
       "      <th>num_1</th>\n",
       "      <th>riqi</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>青岛市</td>\n",
       "      <td>1</td>\n",
       "      <td>HUABEI</td>\n",
       "      <td>24</td>\n",
       "      <td>132</td>\n",
       "      <td>20191005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>青岛市</td>\n",
       "      <td>1</td>\n",
       "      <td>YUEBAO</td>\n",
       "      <td>1</td>\n",
       "      <td>87</td>\n",
       "      <td>20190910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>合肥市</td>\n",
       "      <td>1</td>\n",
       "      <td>YUANDONGRONGZU</td>\n",
       "      <td>24</td>\n",
       "      <td>272</td>\n",
       "      <td>20191007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>481</th>\n",
       "      <td>合肥市</td>\n",
       "      <td>1</td>\n",
       "      <td>YUEBAO</td>\n",
       "      <td>11</td>\n",
       "      <td>247</td>\n",
       "      <td>20190925</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>482</th>\n",
       "      <td>青岛市</td>\n",
       "      <td>1</td>\n",
       "      <td>TIANCHENGRONGZU</td>\n",
       "      <td>10</td>\n",
       "      <td>135</td>\n",
       "      <td>20190913</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>483</th>\n",
       "      <td>合肥市</td>\n",
       "      <td>1</td>\n",
       "      <td>DELEKEJI</td>\n",
       "      <td>1</td>\n",
       "      <td>247</td>\n",
       "      <td>20190925</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>484 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    city  记录数        credit_by  num  num_1      riqi\n",
       "0    青岛市    1           HUABEI   24    132  20191005\n",
       "1    青岛市    1           YUEBAO    1     87  20190910\n",
       "2    合肥市    1   YUANDONGRONGZU   24    272  20191007\n",
       "..   ...  ...              ...  ...    ...       ...\n",
       "481  合肥市    1           YUEBAO   11    247  20190925\n",
       "482  青岛市    1  TIANCHENGRONGZU   10    135  20190913\n",
       "483  合肥市    1         DELEKEJI    1    247  20190925\n",
       "\n",
       "[484 rows x 6 columns]"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 复原索引\n",
    "dt2.reset_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9.重命名行名和列名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T06:10:50.715238Z",
     "start_time": "2019-10-23T06:10:50.690305Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>记录数</th>\n",
       "      <th>credit</th>\n",
       "      <th>nums</th>\n",
       "      <th>num_1</th>\n",
       "      <th>riqi</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>青岛</th>\n",
       "      <td>1</td>\n",
       "      <td>HUABEI</td>\n",
       "      <td>24</td>\n",
       "      <td>132</td>\n",
       "      <td>20191005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>青岛</th>\n",
       "      <td>1</td>\n",
       "      <td>YUEBAO</td>\n",
       "      <td>1</td>\n",
       "      <td>87</td>\n",
       "      <td>20190910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>合肥</th>\n",
       "      <td>1</td>\n",
       "      <td>YUANDONGRONGZU</td>\n",
       "      <td>24</td>\n",
       "      <td>272</td>\n",
       "      <td>20191007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>青岛</th>\n",
       "      <td>1</td>\n",
       "      <td>TIANCHENGRONGZU</td>\n",
       "      <td>4</td>\n",
       "      <td>68</td>\n",
       "      <td>20190909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>合肥</th>\n",
       "      <td>1</td>\n",
       "      <td>HUABEI</td>\n",
       "      <td>187</td>\n",
       "      <td>250</td>\n",
       "      <td>20191001</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      记录数           credit  nums  num_1      riqi\n",
       "city                                             \n",
       "青岛      1           HUABEI    24    132  20191005\n",
       "青岛      1           YUEBAO     1     87  20190910\n",
       "合肥      1   YUANDONGRONGZU    24    272  20191007\n",
       "青岛      1  TIANCHENGRONGZU     4     68  20190909\n",
       "合肥      1           HUABEI   187    250  20191001"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过rename()重命名\n",
    "dt = pd.read_csv('test.csv',index_col='city')\n",
    "idx_rename = {'青岛市':'青岛','合肥市':'合肥'}\n",
    "col_rename = {'credit_by':'credit','num':'nums'}\n",
    "dt.rename(index=idx_rename,columns=col_rename).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 更多\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T06:22:00.807759Z",
     "start_time": "2019-10-23T06:22:00.793800Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['青岛', '青岛市', '合肥', '青岛市', '合肥市']\n"
     ]
    }
   ],
   "source": [
    "dt = pd.read_csv('test.csv',index_col='city')\n",
    "index = dt.index\n",
    "columns = dt.columns\n",
    "index_list = index.tolist()\n",
    "columns_list = columns.tolist()\n",
    "index_list[0] = '青岛'\n",
    "index_list[2] = '合肥'\n",
    "columns_list[0] = '无用的'\n",
    "columns_list[1] = 'credit'\n",
    "print(index_list[:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-23T06:22:34.513639Z",
     "start_time": "2019-10-23T06:22:34.498679Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>无用的</th>\n",
       "      <th>credit</th>\n",
       "      <th>num</th>\n",
       "      <th>num_1</th>\n",
       "      <th>riqi</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>青岛</th>\n",
       "      <td>1</td>\n",
       "      <td>HUABEI</td>\n",
       "      <td>24</td>\n",
       "      <td>132</td>\n",
       "      <td>20191005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>青岛市</th>\n",
       "      <td>1</td>\n",
       "      <td>YUEBAO</td>\n",
       "      <td>1</td>\n",
       "      <td>87</td>\n",
       "      <td>20190910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>合肥</th>\n",
       "      <td>1</td>\n",
       "      <td>YUANDONGRONGZU</td>\n",
       "      <td>24</td>\n",
       "      <td>272</td>\n",
       "      <td>20191007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>青岛市</th>\n",
       "      <td>1</td>\n",
       "      <td>TIANCHENGRONGZU</td>\n",
       "      <td>4</td>\n",
       "      <td>68</td>\n",
       "      <td>20190909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>合肥市</th>\n",
       "      <td>1</td>\n",
       "      <td>HUABEI</td>\n",
       "      <td>187</td>\n",
       "      <td>250</td>\n",
       "      <td>20191001</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     无用的           credit  num  num_1      riqi\n",
       "青岛     1           HUABEI   24    132  20191005\n",
       "青岛市    1           YUEBAO    1     87  20190910\n",
       "合肥     1   YUANDONGRONGZU   24    272  20191007\n",
       "青岛市    1  TIANCHENGRONGZU    4     68  20190909\n",
       "合肥市    1           HUABEI  187    250  20191001"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt.index = index_list\n",
    "dt.columns = columns_list\n",
    "dt.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10.创建、删除列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-24T01:29:42.569357Z",
     "start_time": "2019-10-24T01:29:42.560418Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['记录数', 'city', 'credit_by', 'num', 'num_1', 'riqi', 'ov'], dtype='object')"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt['ov'] = 0\n",
    "dt.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-24T01:25:19.820909Z",
     "start_time": "2019-10-24T01:25:19.810933Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "记录数          0\n",
       "city         0\n",
       "credit_by    0\n",
       "num          0\n",
       "num_1        0\n",
       "riqi         0\n",
       "ov           0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt['ov'] = dt.num + dt.num_1\n",
    "dt.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-24T01:27:29.939406Z",
     "start_time": "2019-10-24T01:27:29.931427Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用all()检查是否所有的布尔值都为True\n",
    "dt['num'].all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-24T01:29:45.566342Z",
     "start_time": "2019-10-24T01:29:45.540410Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>记录数</th>\n",
       "      <th>city</th>\n",
       "      <th>credit_by</th>\n",
       "      <th>num</th>\n",
       "      <th>num_1</th>\n",
       "      <th>riqi</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>青岛市</td>\n",
       "      <td>HUABEI</td>\n",
       "      <td>24</td>\n",
       "      <td>132</td>\n",
       "      <td>20191005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>青岛市</td>\n",
       "      <td>YUEBAO</td>\n",
       "      <td>1</td>\n",
       "      <td>87</td>\n",
       "      <td>20190910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>YUANDONGRONGZU</td>\n",
       "      <td>24</td>\n",
       "      <td>272</td>\n",
       "      <td>20191007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>青岛市</td>\n",
       "      <td>TIANCHENGRONGZU</td>\n",
       "      <td>4</td>\n",
       "      <td>68</td>\n",
       "      <td>20190909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>HUABEI</td>\n",
       "      <td>187</td>\n",
       "      <td>250</td>\n",
       "      <td>20191001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>479</th>\n",
       "      <td>1</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>YUEBAO</td>\n",
       "      <td>2</td>\n",
       "      <td>193</td>\n",
       "      <td>20190902</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>480</th>\n",
       "      <td>1</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>HUABEI</td>\n",
       "      <td>105</td>\n",
       "      <td>193</td>\n",
       "      <td>20190902</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>481</th>\n",
       "      <td>1</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>YUEBAO</td>\n",
       "      <td>11</td>\n",
       "      <td>247</td>\n",
       "      <td>20190925</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>482</th>\n",
       "      <td>1</td>\n",
       "      <td>青岛市</td>\n",
       "      <td>TIANCHENGRONGZU</td>\n",
       "      <td>10</td>\n",
       "      <td>135</td>\n",
       "      <td>20190913</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>483</th>\n",
       "      <td>1</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>DELEKEJI</td>\n",
       "      <td>1</td>\n",
       "      <td>247</td>\n",
       "      <td>20190925</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>484 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     记录数 city        credit_by  num  num_1      riqi\n",
       "0      1  青岛市           HUABEI   24    132  20191005\n",
       "1      1  青岛市           YUEBAO    1     87  20190910\n",
       "2      1  合肥市   YUANDONGRONGZU   24    272  20191007\n",
       "3      1  青岛市  TIANCHENGRONGZU    4     68  20190909\n",
       "4      1  合肥市           HUABEI  187    250  20191001\n",
       "..   ...  ...              ...  ...    ...       ...\n",
       "479    1  合肥市           YUEBAO    2    193  20190902\n",
       "480    1  合肥市           HUABEI  105    193  20190902\n",
       "481    1  合肥市           YUEBAO   11    247  20190925\n",
       "482    1  青岛市  TIANCHENGRONGZU   10    135  20190913\n",
       "483    1  合肥市         DELEKEJI    1    247  20190925\n",
       "\n",
       "[484 rows x 6 columns]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt = dt.drop('ov',axis='columns')\n",
    "dt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 更多\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-24T01:34:10.367195Z",
     "start_time": "2019-10-24T01:34:10.339267Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>记录数</th>\n",
       "      <th>city</th>\n",
       "      <th>credit_by</th>\n",
       "      <th>num</th>\n",
       "      <th>p</th>\n",
       "      <th>num_1</th>\n",
       "      <th>riqi</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>青岛市</td>\n",
       "      <td>HUABEI</td>\n",
       "      <td>24</td>\n",
       "      <td>108</td>\n",
       "      <td>132</td>\n",
       "      <td>20191005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>青岛市</td>\n",
       "      <td>YUEBAO</td>\n",
       "      <td>1</td>\n",
       "      <td>86</td>\n",
       "      <td>87</td>\n",
       "      <td>20190910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>YUANDONGRONGZU</td>\n",
       "      <td>24</td>\n",
       "      <td>248</td>\n",
       "      <td>272</td>\n",
       "      <td>20191007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>青岛市</td>\n",
       "      <td>TIANCHENGRONGZU</td>\n",
       "      <td>4</td>\n",
       "      <td>64</td>\n",
       "      <td>68</td>\n",
       "      <td>20190909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>HUABEI</td>\n",
       "      <td>187</td>\n",
       "      <td>63</td>\n",
       "      <td>250</td>\n",
       "      <td>20191001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>479</th>\n",
       "      <td>1</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>YUEBAO</td>\n",
       "      <td>2</td>\n",
       "      <td>191</td>\n",
       "      <td>193</td>\n",
       "      <td>20190902</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>480</th>\n",
       "      <td>1</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>HUABEI</td>\n",
       "      <td>105</td>\n",
       "      <td>88</td>\n",
       "      <td>193</td>\n",
       "      <td>20190902</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>481</th>\n",
       "      <td>1</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>YUEBAO</td>\n",
       "      <td>11</td>\n",
       "      <td>236</td>\n",
       "      <td>247</td>\n",
       "      <td>20190925</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>482</th>\n",
       "      <td>1</td>\n",
       "      <td>青岛市</td>\n",
       "      <td>TIANCHENGRONGZU</td>\n",
       "      <td>10</td>\n",
       "      <td>125</td>\n",
       "      <td>135</td>\n",
       "      <td>20190913</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>483</th>\n",
       "      <td>1</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>DELEKEJI</td>\n",
       "      <td>1</td>\n",
       "      <td>246</td>\n",
       "      <td>247</td>\n",
       "      <td>20190925</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>484 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     记录数 city        credit_by  num    p  num_1      riqi\n",
       "0      1  青岛市           HUABEI   24  108    132  20191005\n",
       "1      1  青岛市           YUEBAO    1   86     87  20190910\n",
       "2      1  合肥市   YUANDONGRONGZU   24  248    272  20191007\n",
       "3      1  青岛市  TIANCHENGRONGZU    4   64     68  20190909\n",
       "4      1  合肥市           HUABEI  187   63    250  20191001\n",
       "..   ...  ...              ...  ...  ...    ...       ...\n",
       "479    1  合肥市           YUEBAO    2  191    193  20190902\n",
       "480    1  合肥市           HUABEI  105   88    193  20190902\n",
       "481    1  合肥市           YUEBAO   11  236    247  20190925\n",
       "482    1  青岛市  TIANCHENGRONGZU   10  125    135  20190913\n",
       "483    1  合肥市         DELEKEJI    1  246    247  20190925\n",
       "\n",
       "[484 rows x 7 columns]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用insert()方法原地插入列\n",
    "p_index = dt.columns.get_loc('num') + 1\n",
    "p_index\n",
    "dt.insert(loc=p_index,\n",
    "         column='p',\n",
    "         value=dt['num_1'] - dt['num'])\n",
    "dt"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
